/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dinotrack.struts;

import com.dinotrack.domain.Usuario;
import com.dinotrack.domain.UsuarioSQL;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.chain.contexts.ActionContext;

/**
 *
 * @author alquad
 */
public class LoginAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private final static String FAILURE = "failure";
    private final static String LOGOUT = "logout";
    private final static String CONTROL_PANEL = "controlPanel";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        LoginForm formBean = (LoginForm) form;

        if (request.getParameter("login") != null) {
            String usuario = formBean.getUsuario();
            String contraseña = formBean.getContrasenia();

            if ((usuario == null) || contraseña == null || usuario.equals("") || contraseña.equals("")) {
                formBean.setError();
                return mapping.findForward(FAILURE);
            }

            Usuario user = new Usuario();
            boolean login;
            BeanUtils.copyProperties(user, form);

            UsuarioSQL usuarioSQL = new UsuarioSQL();
            login = usuarioSQL.checkUser(user);

            if (login) {
                HttpSession session = request.getSession(true);
                session.setAttribute("USER", usuario);
                formBean.setLoginResult(LoginForm.LOGIN);
                System.out.println("########################## SESSION - USER: " + session.getAttribute("USER"));
            }

//        Struts 2
//        Map session = ActionContext.getContext().getSession();
//        session.put("nombre", "valor");

            return mapping.findForward(SUCCESS);
        }

        if (request.getParameter("controlPanel") != null) {
            return mapping.findForward(CONTROL_PANEL);
        }

        if (request.getParameter("logout") != null) {
            HttpSession session = request.getSession(true);
            session.setAttribute("USER", null);
            formBean.setLoginResult(LoginForm.NOT_LOGIN);
            System.out.println("########################## SESSION - USER: " + session.getAttribute("USER"));

            return mapping.findForward(LOGOUT);
        }

        return mapping.findForward(SUCCESS);
    }
}
